package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.CategoryChildrenDto;
import cevent.source.cloudcenter.server.dto.CategoryDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.CategoryService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 总分类表控制层
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 总分类表";
    @Resource
    private CategoryService categoryService;

    //1.dto-category获取
    @RequestMapping("/dtoList")
    public ResponseDataDto getDtoList() {
        ResponseDataDto responseData=new ResponseDataDto();
        List<CategoryDto> categoryChildrenDtoList= categoryService.categoryDtoList();
        responseData.setResponseData(categoryChildrenDtoList);
        return responseData;
    }

    //tree结构分类
    @RequestMapping("/treeList")
    public ResponseDataDto getTreeList(){
        ResponseDataDto responseData=new ResponseDataDto();
        List<CategoryChildrenDto> categoryDtoList=categoryService.categoryTreeList();
        responseData.setResponseData(categoryDtoList);
        return responseData;
    }

    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        categoryService.getCategoryPageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody CategoryDto categoryDto) {
        LOG.info("传入的category DTO：{}", categoryDto);

        ValidatorUtil.requiredEmpty(categoryDto.getName(), "名称");
        ValidatorUtil.requiredLength(categoryDto.getName(), "名称", 3, 255);
        ValidatorUtil.requiredEmpty(categoryDto.getParentId(), "父ID");
        ResponseDataDto responseData = new ResponseDataDto();
        categoryService.save(categoryDto);
        responseData.setResponseData(categoryDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的category uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        categoryService.delete(uniId);
        return responseData;
    }

    /**5.根据uniId获取category
     */
    @RequestMapping("/info/{uniId}")
    public ResponseDataDto info(@PathVariable("uniId") String uniId){
        ResponseDataDto responseData=new ResponseDataDto();
        CategoryDto categoryDto= categoryService.getCategoryInfo(uniId);
        responseData.setResponseData(categoryDto);
        return responseData;
    }
    //6.拖拽接口：批量修改level/sort和parentId
    @RequestMapping("/update/level")
    public ResponseDataDto updateLevel(@RequestBody CategoryDto[] categoryDtos){
        ResponseDataDto responseData=new ResponseDataDto();
        //返回collection集合，需要asList转换
        responseData.setResponseData(categoryService.updateLevelByUniId(categoryDtos));
        return responseData;
    }

    //7.批量删除
    @PostMapping("/delBatch")
    public ResponseDataDto delBatch(@RequestBody String[] uniIds){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(categoryService.delBatchCategory(uniIds));
        return responseData;
    }

    //8.根据ModuleCategory获取到的dtoList中的categoryUniIds获取categoryChildrenDtoList
    //@RequestBody参数类型为string，发送请求直接字符串，不用加""
    @PostMapping("/module/treeList/{categoryUniId}")
    public ResponseDataDto getModuleTreeList(@PathVariable(value = "categoryUniId") String categoryUniId){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(categoryService.getCategoryChildrenList(categoryUniId));
        return responseData;
    }
}
